package jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


/**
 * 媒婆
 *
 * @author cctv
 */
public class MeiPo implements InvocationHandler {
    private Person target;

    /**
     * 获取代理人个人资料
     *
     * @param target
     * @return
     */
    public Object getInstance(Person target) {
        this.target = target;
        Class clazz = target.getClass();
        System.out.println("原始对象："+clazz);
        return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this);
    }


    /**
     * 代理执行
     *
     * @param proxy
     * @param method
     * @param args
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("我是媒婆：代理人是" + target.getName() + "(" + target.getSex() + ")");
        System.out.println("开始海选……");
//        this.target.findLove();
        // 相等效果 因为$Proxy 里的findlove是super.h.invoke(this, m4, (Object[])null);
        method.invoke(target,args);
        System.out.println("匹配上了");

        return null;
    }
}
